/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Alejandro
 */
@Entity
@Table(name = "RS_ITEMS")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Item.findAll", query = "SELECT r FROM Item r ORDER BY r.registrationDate DESC"),
    @NamedQuery(name = "Item.findAllActives", query = "SELECT r FROM Item r WHERE r.status>0 ORDER BY r.registrationDate DESC "),
    @NamedQuery(name = "Item.findByIdItem", query = "SELECT r FROM Item r WHERE r.idItem = :idItem"),
    @NamedQuery(name = "Item.findByName", query = "SELECT r FROM Item r WHERE r.name = :name"),
    @NamedQuery(name = "Item.findByDescription", query = "SELECT r FROM Item r WHERE r.description = :description"),
    @NamedQuery(name = "Item.findByPrice", query = "SELECT r FROM Item r WHERE r.price = :price"),
    @NamedQuery(name = "Item.findByStatus", query = "SELECT r FROM Item r WHERE r.status = :status"),
    @NamedQuery(name = "Item.findByIdCategory", query = "SELECT r FROM Item r WHERE r.idCategory = :idCategory AND r.status>0")})
public class Item implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @GeneratedValue
    @Column(name = "ID_ITEM")
    private Integer idItem;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "NAME")
    private String name;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 2000)
    @Column(name = "DESCRIPTION",length = 2000)
    private String description;
    @Basic(optional = false)
    @NotNull
    @Column(name = "PRICE")
    private Double price;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "PICTURE")
    private String picture;
    @Basic(optional = false)
    @NotNull
    @Column(name = "STATUS")
    private Integer status;
   
    @Basic(optional = false)
    @NotNull
    @Column(name = "REGISTRATION_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date registrationDate;
    
    
    
    /*
    @Basic(optional = false)
    @NotNull
    @Column(name = "ID_CATEGORY")
    private Integer idCategory;
*/
    @ManyToOne
    @JoinColumn(name="ID_CATEGORY", nullable = false)
    private Category idCategory;
    
    @OneToMany (mappedBy="item")
    private List<ItemPurchased> itemPurchase;
    
    public Item() {
        this.picture = "recursos/images/z_discoduro.jpg";
        this.status=1;
    }

    public Item(Integer idItem) {
        this.idItem = idItem;
    }
//public Item(String description, String name, Double price, Date registrationDate){
//     this.idItem = ;//TODO
//        this.name = name;
//        this.description = description;
//        this.price = price;
//        this.status = status;
//        this.idCategory = idCategory;
//}

    public Item(String name, String description, Double price, Category idCategory, String nombreImagen) {
        
        this.name = name;
        this.description = description;
        this.price = price;
        this.picture = nombreImagen;
        this.status = 1;
        this.registrationDate = new Date();
        this.idCategory = idCategory;
        
    }
    
    public Integer getIdItem() {
        return idItem;
    }

    public void setIdItem(Integer idItem) {
        this.idItem = idItem;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Date getRegistrationDate() {
        return registrationDate;
    }

    public void setRegistrationDate(Date registrationDate) {
        this.registrationDate = registrationDate;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public List<ItemPurchased> getItemPurchase() {
        return itemPurchase;
    }

    public void setItemPurchase(List<ItemPurchased> itemPurchase) {
        this.itemPurchase = itemPurchase;
    }




    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Category getIdCategory() {
        return idCategory;
    }

    public void setIdCategory(Category idCategory) {
        this.idCategory = idCategory;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idItem != null ? idItem.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Item)) {
            return false;
        }
        Item other = (Item) object;
        if ((this.idItem == null && other.idItem != null) || (this.idItem != null && !this.idItem.equals(other.idItem))) {
            return false;
        }
        return true;
    }
    
    public boolean stock(){
        return status.equals(1);
    }
    
    public boolean reposicion(){
        return status.equals(2);
    }
    
    public boolean mostrar(){
        return !status.equals(0);
    }
            

    @Override
    public String toString() {
        return "entities.Item[ idItem=" + idItem + " ]";
    }
    
}
